package com.liang.spring.test.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.liang.spring.test.controller.entity.ParamInfoExample;
import com.liang.spring.test.controller.entity.ParamInfo;
import com.liang.spring.test.controller.entity.TrainInfoExample;
import com.liang.spring.test.controller.entity.UserInfo;
import com.liang.spring.test.controller.entity.UserInfoExample;
import com.liang.spring.test.controller.entity.UserInfoExample.Criteria;
import com.liang.spring.test.dao.ParamInfoMapper;
import com.liang.spring.test.dao.UserInfoMapper;

/**
 * @
 * @author L
 */

@Controller
@RequestMapping(value = "/userInfo")
public class userController {

	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private ParamInfoMapper paramInfoMapper;
	
	private int fid;
	/**
	 * 检查用户名是否可注册
	 * @param fcertificateno 身份证号
	 * @param printWriter
	 * @return
	 */
	@RequestMapping(value = "/checkFusername")
	public String checkFusername(@RequestParam("fusername") String fusername, PrintWriter printWriter) {
		UserInfoExample example = new UserInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andFusernameEqualTo(fusername.replace(",", ""));
		int count = userInfoMapper.countByExample(example);
		String jsonString = JSON.toJSONString(count == 0 ? "可以注册" : "已被注册", SerializerFeature.PrettyFormat);  
        printWriter.write(jsonString);  
        printWriter.flush();  
        printWriter.close();  
		return "/reg";
	}
	
	/**
	 * 检查身份证号是否可注册
	 * @param fcertificateno 身份证号
	 * @param printWriter
	 * @return
	 */
	@RequestMapping(value = "/checkFcertificateno")
	public String checkFcertificateno(@RequestParam("fcertificateno") String fcertificateno, PrintWriter printWriter) {
		UserInfoExample example = new UserInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andFcertificatenoEqualTo(fcertificateno.replace(",", ""));
		int count = userInfoMapper.countByExample(example);
		String jsonString = JSON.toJSONString(count == 0 ? "可以注册" : "已被注册", SerializerFeature.PrettyFormat);  
        printWriter.write(jsonString);  
        printWriter.flush();  
        printWriter.close();  
		return "/reg";
	}
	
	/**
	 * 登录
	 * @param model
	 * @param username
	 * @param password
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login")
	public String logon(Model model, @RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
		UserInfoExample example = new UserInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andFusernameEqualTo(username);
		criteria.andFpasswordEqualTo(password);
		int count = userInfoMapper.countByExample(example);
		if (count != 0) {
			UserInfo user = userInfoMapper.selectByPrimary(userInfoMapper.selectIdByUsernameAndPassword(username, password));
			session.setAttribute("user", user);
			
			ParamInfo param = paramInfoMapper.selectByPrimaryKey(1);
			session.setAttribute("sessionParam", param);
		} else {
			model.addAttribute("result", "用户名、密码错误！");
		}
		return "/index";
	}
	
	/**
	 * 注销
	 * @param model
	 * @param username
	 * @param password
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/logoff")
	public String logoff(HttpSession session) {
		session.removeAttribute("user");
		return "/index";
	}
	
	/**
	 * 用户注册
	 * @param model
	 * @param userinfo
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ModelAndView add(Model model, UserInfo userinfo){
		try {		
			TrainInfoExample example = new TrainInfoExample();
			fid = userInfoMapper.selectMaxId() + 1;
			userinfo.setFid(fid);
			userInfoMapper.insert(userinfo);
			model.addAttribute("result", "注册成功！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return new ModelAndView("/msg");
	}
	
	/**
	 * 用户修改信息
	 * @param userinfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(UserInfo userinfo, HttpSession session){
		try {		
			UserInfoExample example = new UserInfoExample();
			Criteria criteria = example.createCriteria();
			int fid = (int) ((UserInfo) session.getAttribute("user")).getFid();
			criteria.andFidEqualTo(fid);
			userinfo.setFid(fid);
			userInfoMapper.updateByExample(userinfo, example);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/updateUser";
	}
	
	/**
	 * 更新用户的信用级别
	 *//*
	@RequestMapping(value = "/updatePersonalcredit")
	public String updatePersonalcredit(UserInfo userinfo){
		try {		
			userInfoMapper.updatePersonalcredit(userinfo.getPersonalcredit(), userinfo.getFid());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/updatePersonalcredit";
	}*/
	
	/**
	 * 查询所有用户信息
	 * @param model
	 * @param userinfo
	 * @return
	 */
	@RequestMapping(value = "/selAll")
	public String selAll(@RequestParam("selNameOrCode") String selNameOrCode,@RequestParam("txt") String txt, Model model, UserInfo userinfo){
		try {		
			UserInfoExample example = new UserInfoExample();
			Criteria criteria = example.createCriteria();
			if (txt.trim().length() != 0) {
				if (selNameOrCode.replaceAll(",", "").equalsIgnoreCase("姓名")) {
					criteria.andFealnameLike("%" + txt + "%");
				} else {
					criteria.andFcertificatenoLike("%" + txt + "%");
				}
			}
			criteria.andFealnameNotEqualTo("admin");
			List<UserInfo> userList = userInfoMapper.selectByExample(example);		
			model.addAttribute("userList", userList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/personalcredit";
	}
	
	/**
	 * 根据id查询用户信息
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/selById")
	public String selById(Model model, HttpSession session){
		try {		
			UserInfoExample example = new UserInfoExample();
			Criteria criteria = example.createCriteria();
			String username = (String) ((UserInfo) session.getAttribute("user")).getFusername();
			String password = (String) ((UserInfo) session.getAttribute("user")).getFpassword();
			criteria.andFusernameEqualTo(username);
			criteria.andFpasswordEqualTo(password);
			List<UserInfo> userList = userInfoMapper.selectByExample(example);
			model.addAttribute("userList", userList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/updateUser";
	}
	
	@RequestMapping(value = "/selById2")
	public String selById2(String trainnumber, Model model){
		try {		
			UserInfoExample example = new UserInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andFidEqualTo(Integer.parseInt(trainnumber));
			List<UserInfo> userList = userInfoMapper.selectByExample(example);
			model.addAttribute("userList", userList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/updatePersonalcredit";
	}
}
